<script setup>
  import { ElMessage } from 'element-plus'
  import { User, Lock, Hide, View } from '@element-plus/icons-vue'
  import { reactive, onUpdated } from 'vue';
  import { setStorage, getStorage } from '@/utils/storage'
  import useAppStatusStore from '@/stores/appStatus'
  import useUserInfoStore from '@/stores/userinfo'
  const appStatusStore = useAppStatusStore()
  const userInfoStore = useUserInfoStore()

  const userAccountKey = 'ruizhiUserAccount'

  // 表单内容
  const formData = reactive({
    account: '',
    password: '',
    showPassword: false,
    hasEncrypted: false,
    isRemember: false,
  })

  const toLogin = () => {
    if (formData.isRemember) {
      setStorage(
        {account: formData.account, password: formData.password}, 
        userAccountKey
      )
    } else {
      setStorage(null, userAccountKey)
    }
    userInfoStore.appUserLogin(formData).then((res) => {
      ElMessage.success(res.message)
      appStatusStore.showLogin = false
      // 登录成功后，紧接着获取信息
      userInfoStore.getAppUserInfo()
    })
  }

  const toApplyRegister = () => {
    appStatusStore.showLogin = false
    appStatusStore.showRegister = true
  }

  onUpdated(() => {
    if (appStatusStore.showLogin) {
      const userAccount = getStorage(userAccountKey)
      if (userAccount) {
        // console.log(userAccount)
        formData.account = userAccount.account
        formData.password = userAccount.password
        formData.isRemember = true
      } else {
        formData.account = ''
        formData.password = ''
        formData.isRemember = false
      }
    }
  })

</script>
<template>
  <el-dialog
    v-model="appStatusStore.showLogin"
    class="login-dialog"
    align-center
    append-to-body
    width="400px"
    :show-close="false"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <template #header>
      <div class="title">帐号登录</div>
    </template>
    <div class="content">
      <el-form :model="formData" label-width="0">
        <el-form-item prop="account">
          <el-input
            v-model="formData.account"
            clearable
            placeholder="手机号/用户名"
            class="form-input"
          >
          <template #prefix>
            <el-icon><User /></el-icon>
          </template>
        </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formData.password"
            placeholder="密码"
            class="form-input"
            :type="formData.showPassword ? 'text' : 'password'"
            @keyup.enter="toLogin"
          >
            <template #prefix>
              <el-icon><Lock /></el-icon>
            </template>
            <template #suffix>
              <el-icon @click="formData.showPassword = !formData.showPassword">
                <View v-if="formData.showPassword" /> 
                <Hide v-else />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
      </el-form>
      <div>
        <el-checkbox v-model="formData.isRemember" label="记住密码" />
      </div>
    </div>
    <template #footer>
      <div class="footer">
        <div class="apply-account">
          <span>还没有帐号?</span>
          <span class="apply-btn" @click="toApplyRegister">申请试用</span>
        </div>
        <el-button
          type="primary"
          class="action-btn"
          :loading="userInfoStore._loginLoading"
          @click="toLogin"
        >
          登录
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<style scoped lang="scss">
  .login-dialog{
    .title{
      text-align: center;
      color: #222;
      font-weight: 600;
      font-size: 18px;
      margin: -20px -36px -20px -20px;
      padding: 20px 30px 8px 30px;
    }
    .content {
      margin: -20px -20px -40px -20px;
      padding: 38px 30px 1px 30px;
      .form-input{
        height: 42px;
        font-size: 16px;
      }
    }
    .footer {
      padding: 8px 30px 20px 30px;
      margin: 0px -20px -20px -20px;
      display: flex;
      align-items: center;
      .apply-account {
        font-size: 12px;
        .apply-btn {
          color: $goldenColor;
          margin-left: 2px;
          text-decoration: underline;
          cursor: pointer;
        }
      }
      .action-btn {
        margin-left: auto;
        height: 30px;
        width: 60px;
        font-size: 15px;
      }
    }
  }
  
</style>